package com.linwen.javase.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassName _11RunnableCallableThread
 * @Description
 * @Author lin
 * @Date 2022/4/21 下午11:15
 * @Version V1.0
 * Runnable 和 Callable 接口实现线程类
 *
 * Runnable 返回值为void   Callable返回值为Object
 * Runnable中方法为run   Callable中方法文call
 * run方法中不能抛异常  call中可以抛异常
 * 实现Runnable接口的线程可以直接传给Thread，实现Callable接口的类需要借助FutureTask
 */
public class _11RunnableCallableThread {
    public static void main(String[] args) {
        new Thread(new MyRunnable(),"runnable").start();
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask,"callable").start();
        while (!futureTask.isDone()){
            System.out.println(Thread.currentThread().getName()+" watting,,,,,,");
        }
        try {
            System.out.println(futureTask.get());
            while (!futureTask.isDone()){
                System.out.println("========");
            }
            System.out.println(futureTask.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

    }
}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"  myRunnable");
    }
}

class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"  myCallable");
        return 1024;
    }
}
